/*!
//      
//      \file	k2k_StateHandlerOff.c
//      
//      \author	Copyright 2010 Valentina Vuksic <vuksv1@bfh.ch>, Beat Liechti <liecb2@bfh.ch>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
//
//      Abstract Data Type for the state handler of the state "Off"
//      
//      \brief		CAS-EBX-03 Projektarbeit 
//      			Valentina Vuksic (VUKSV1), Beat Liechti (LIECB2)
//      			StateHandlerOff ADT for the k2k (Kaffee2000) application
//      \version	1.0
//      \date		2010-04-11
//      \remark		Last Modifications:
//      \remark  	v1.0, VUKSV1/LIECB2, 2010-04-11 Initial release
//
*/

#include "k2k_StateHandlerOff.h"

#define STATE_TEXT	"Off"

/* -- static function declarations -----------------------------------*/

static k2k_State k2k_StateHandlerOff_Handler(k2k_Event event, k2k_State previousState, k2k_StateHandlerData *handlerData);

/* -- ADT functions --------------------------------------------------*/

/*!
//
// Creates StateHandlerOff ADT "object"
//
// \param stateMachine is the pointer to the StateMachine structure 
//
// \return pointer to the StateHandlerOff ADT
//
*/
k2k_StateHandlerOff* k2k_StateHandlerOff_Create(k2k_StateMachine *stateMachine)
{
	k2k_StateHandlerOff *stateHandlerOff = NULL;
	stateHandlerOff = malloc(sizeof(k2k_StateHandlerOff));
	if (stateHandlerOff != NULL)
	{
		/*! initialize structure */		
		stateHandlerOff->handler = k2k_StateHandlerOff_Handler;
		/*! create state handler data */
		stateHandlerOff->handlerData = k2k_StateHandlerData_Create(stateHandlerOff);		
		if (stateHandlerOff->handlerData != NULL)
		{
			/*! add the state handler to the state machine */
			k2k_StateMachine_AddStateHandler(stateMachine, STATE_OFF, stateHandlerOff->handler, stateHandlerOff->handlerData);
			/*! store reference to state machine */
			stateHandlerOff->stateMachine = stateMachine;
		}
	}
	return stateHandlerOff;
}

/*!
//
// Releases StateHandlerOff ADT "object"
// 
// \param stateHandlerOff is the pointer to the StateHandlerOff structure 
// 
// \return nothing
//
*/
void k2k_StateHandlerOff_Release(k2k_StateHandlerOff *stateHandlerOff)
{
	if (stateHandlerOff != NULL)
	{				
		if (stateHandlerOff->handlerData != NULL)
		{
			/*! release handler data */
			k2k_StateHandlerData_Release(stateHandlerOff->handlerData);
			stateHandlerOff->handlerData = NULL;
		}
		/*! release memory */
		free(stateHandlerOff);	
	}
}

/* -- static functions -----------------------------------------------*/

/*!
//
// Actual state handler for the state "Off"
//
// \param event describes the event that happened
// \param customData points to custom data (typically the corresponding ADT struct)
//
// \return next state of the event machine
//
*/
static k2k_State k2k_StateHandlerOff_Handler(k2k_Event event, k2k_State previousState, k2k_StateHandlerData *handlerData)
{
	/*! get the state handler structure */
	k2k_StateHandlerOff *stateHandlerOff = handlerData->customData;
	
	/*! check if entering this state */
	if (previousState != STATE_OFF)
	{
		/*! set "off" coloring for on/off indicator */
		k2k_NanoX_DrawOnOffButton(stateHandlerOff->stateMachine->nanoX, LTRED);
		/*! set state text */
		k2k_NanoX_ShowStateText(stateHandlerOff->stateMachine->nanoX, STATE_TEXT);
		/*! hide Rstr text */
		k2k_NanoX_HideRstrText(stateHandlerOff->stateMachine->nanoX);
		/*! hide Espr text */
		k2k_NanoX_HideEsprText(stateHandlerOff->stateMachine->nanoX);
		/*! hide MlkC text */
		k2k_NanoX_HideMlkCText(stateHandlerOff->stateMachine->nanoX);
		/*! hide coffee cups */
		k2k_NanoX_DrawCups(stateHandlerOff->stateMachine->nanoX, BLACK, BLACK);		
	}
	/*! the default is to remain in the same state */
	int nextState = STATE_OFF;	
	if (event == EVENT_S0)
	{
		/*! set "off" coloring for on/off indicator */
		k2k_NanoX_DrawOnOffButton(stateHandlerOff->stateMachine->nanoX, LTGREEN);		
		/*! next state is Rdy1 */
		nextState = STATE_RDY1;
	}
	return nextState;
}
